package edu.gatech.oad.schedule;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import edu.gatech.oad.user.AccountsController;
import edu.gatech.oad.user.MedicalPersonnelSchedule;
import edu.gatech.oad.user.MedicalPersonnelAvailability;
import edu.gatech.oad.user.Patient;
import edu.gatech.oad.user.Doctor;


/**
 * Handles the searching for open periods of time, 
 * between the Doctor's schedule and the patient's 
 * open time
 * @author Joey Slater
 * @version 1.00
 */
public class CalendarController {
	private final Set<Doctor> doctors =  AccountsController.sharedController().getDoctors();
	private List<MedicalPersonnelSchedule> doctorSchedule = new ArrayList<MedicalPersonnelSchedule>();
	private List<MedicalPersonnelAvailability> availableSchedule = new ArrayList<MedicalPersonnelAvailability>();
	private static CalendarController sharedCalendarController;
	/**
	 * Adding appointment and doctor's appointments
	 * @param appts the list of all appointment
	 */
	private CalendarController(){
		for(Doctor docs : doctors){
			doctorSchedule.add(docs.getSchedule());
		}
	}
	
	public static CalendarController sharedCalendarController(){
		if (sharedCalendarController == null) {
			sharedCalendarController = new CalendarController();
		}
		return sharedCalendarController;
	}

	/**
	 * The method for checking the patient into the appointment
	 * @param patient patient whose appointment it is 
	 * @param appointment the appointment being checked into
	 */ 
	public void checkIn(Patient patient, Appointment appointment){
		appointment.changeCheckIn();
	}
	
	//Functions that search for open times
	//TODO Change the following to return the appropriate 
	public List<MedicalPersonnelAvailability> search(int day, int month, int year){
		for(Doctor docs : doctors){
			for(MedicalPersonnelAvailability mpa : docs.getSchedule().getAvailabilities()){
				if((mpa.getMonth() == month) && (mpa.getDay() == day) && (mpa.getYear() == year)){
					availableSchedule.add(mpa);
					System.out.println(true);
				}
			}
		}
		return availableSchedule;
	} //Returns the times
	public List<MedicalPersonnelAvailability> search(Doctor doctor){
		for(Doctor docs : doctors){
			if(docs.compareTo(doctor) == 0){
				availableSchedule = docs.getSchedule().getAvailabilities();
			}
		}
		return availableSchedule;
	} //Returns the dates 
	public List<MedicalPersonnelAvailability> search(int month, int day, int year, Doctor doctor){
		for(Doctor docs : doctors){
			if(docs.equals(doctor)){
				for(MedicalPersonnelAvailability mpa : doctor.getSchedule().getAvailabilities()){
					if(mpa.getMonth() == month && mpa.getDay() == day && mpa.getYear() == year){
						availableSchedule.add(mpa);
						System.out.println(true);
					}
				}
			}
		}
		return availableSchedule;
	} //Return the  

	//Gets all
	public List<Appointment> getAllAppointments(){
		List<Appointment> allAppts = new ArrayList<Appointment>();
		for(Patient patient : AccountsController.sharedController().getPatients()){
			for(Appointment appt : patient.getAppointments()){
				allAppts.add(appt);
			}
		}
		return allAppts;	
	}
	
	//Gets all
	public List<Appointment> getAppointmentsThatDay(){
		Calendar cal = GregorianCalendar.getInstance();
		Calendar apptCal = GregorianCalendar.getInstance();
		List<Appointment> allAppts = new ArrayList<Appointment>();
		for(Patient patient : AccountsController.sharedController().getPatients()){
			for(Appointment appt : patient.getAppointments()){
				apptCal.setTime(appt.getDate());
				if((apptCal.get(Calendar.DATE) == cal.get(Calendar.DATE)) &&
						(apptCal.get(Calendar.MONTH) == cal.get(Calendar.MONTH)) &&
						(apptCal.get(Calendar.YEAR) == cal.get(Calendar.YEAR))){
					allAppts.add(appt);
				}
			}
		}
		return allAppts;	
	}
}
